package com.epalmpay.controller.shop.mall;

import com.epalmpay.commom.BaseController;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.mall.MallGoodListOutDTO;
import com.epalmpay.dto.mall.MallGoodOutDTO;
import com.epalmpay.dto.mall.MallGoodQueryDTO;
import com.epalmpay.dto.mall.MallGoodUpdateDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.MallGood;
import com.epalmpay.entity.Page;
import com.epalmpay.service.mall.IMallGoodService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lifayi on 2017/7/10.
 */
@RestController
@RequestMapping("/shop/mall")
public class ShopMallGoodController extends BaseController{

    @Autowired
    private IMallGoodService mallGoodService;

    @RequestMapping("/listMallGood")
    public MallGoodListOutDTO listMallGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<MallGoodOutDTO>();

        Page page = Tools.pageEmtity(pageDTO);

        List<MallGood> mallGoodList;

        try {
            mallGoodList = mallGoodService.listShopMallGood(mallGoodQueryDTO, page);
            for(int i = 0; i < mallGoodList.size(); i++) {
                MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
                PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodOutDTO);
                mallGoodOutDTO.setPicture(mallGoodOutDTO.getPicture().split(",")[0]);   //只要第一张图片的url
                mallGoodOutDTOList.add(mallGoodOutDTO);
            }
            mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
            mallGoodListOutDTO.setPage(page);
            mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
        } catch (ParseException e) {
            logger.error("user parameter parse error");
            e.printStackTrace();
        } catch (Exception e) {
            mallGoodListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
            e.printStackTrace();
        }
        return mallGoodListOutDTO;
    }


    @RequestMapping("/listAuditMallGood")
    public MallGoodListOutDTO listAuditMallGood(MallGoodQueryDTO mallGoodQueryDTO, PageDTO pageDTO) {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();
        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<MallGoodOutDTO>();

        Page page = Tools.pageEmtity(pageDTO);

        List<MallGood> mallGoodList;

        try {
            mallGoodList = mallGoodService.listAuditShopMallGood(mallGoodQueryDTO, page);
            for(int i = 0; i < mallGoodList.size(); i++) {
                MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
                PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodOutDTO);
                mallGoodOutDTO.setPicture(mallGoodOutDTO.getPicture().split(",")[0]);   //只要第一张图片的url
                mallGoodOutDTOList.add(mallGoodOutDTO);
            }
            mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
            mallGoodListOutDTO.setPage(page);
            mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
        } catch (ParseException e) {
            logger.error("user parameter parse error");
            e.printStackTrace();
        } catch (Exception e) {
            mallGoodListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
            e.printStackTrace();
        }
        return mallGoodListOutDTO;
    }

    @RequestMapping("/updateMallGood")
    public BaseOutDTO updateMallGood(@RequestBody MallGoodUpdateDTO mallGoodUpdateDTO) {
        try {
            return mallGoodService.updateShopMallGood(mallGoodUpdateDTO);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }
}
